package cn.dengta.context.model;

import java.io.Serializable;
import java.util.function.Function;

import cn.dengta.common.model.Bean;
import lombok.Getter;

@Getter
public class Option<V> implements Serializable {

	private static final long serialVersionUID = 1L;

	public V value;
	public String label;

	public Long userId;
	public Long pId;

	public Option() {
	}

	public Option(V value, String label, Long pId) {
		this.value = value;
		this.label = label;
		this.pId = pId;
	}

	public Option(V value, String label) {
		this(value, label, null);
	}


	public Option<V> buildUserId(Long userId){
		this.userId = userId;
		return this;
	}

	public static <T, E> Function<T, Option<E>> converter(Function<T, E> valueMapper, Function<T, String> labelMapper) {
		return t -> new Option<>(valueMapper.apply(t), labelMapper.apply(t));
	}

	public static <T extends Bean<?>> Function<T, Option<Long>> converter(Function<T, String> labelMapper) {
		return t -> new Option<>((Long) t.getId(), labelMapper.apply(t));
	}

	public static <T extends Bean<?>> Function<T, Option<Long>> converterWithParent(Function<T, String> labelMapper, Function<T, Long> parentMapper) {
		return t -> new Option<>((Long) t.getId(), labelMapper.apply(t), parentMapper.apply(t));
	}

}
